﻿package  
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author kot
	 * 
	 */
	
	public class Cell extends MovieClip {
		var nameX : int;
		var nameY : int;
		var isAvailable : Boolean;			//подсвечена ли клетка для доски
		var size; 							// размер для отображения
		private var figure: Figure = null; 	//figure
		var isWhite : Boolean;				//цвет клетки
		var isFog : Boolean;				//в тени или нет
		
		public function Cell() {
			isFog = false;
			stop();
			addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		public function onDown(evt) {
			if (figure == null) {
				return; 
			} else {
				figure.onMouseDown(evt);
			}
		}
		
		/// ставит фигуру на клетку
		public function addFigure(newFigure : Figure ) : void {
			if (newFigure == null) {
				return;
			}
			if (figure != null) {
				removeFigure();
			}
			figure = newFigure;
			figure.figureX = nameX;
			figure.figureY = nameY;
			resetFigure(figure);
		}
		
		public function resetFigure(figure : Figure) : void {
			figure.x = x + size / 2;
			figure.y = y + size / 2;
		}
		
		public function removeFigure() : void {
			figure = null;
		}
		public function setFog(hasFog : Boolean) : void {
			isFog = hasFog;
			reDraw();
		}
		
		public function setAvaliable(hasAvailable : Boolean) : void {
			isAvailable = hasAvailable;
			reDraw();
		}
		
		public function reDraw() :void {
			if (isFog) {
				gotoAndStop(3);
				return;
			}
			if (isWhite && isAvailable) {
				gotoAndStop(6);
			}else if (isWhite && (!isAvailable) ) {
				gotoAndStop(1);
			}else if ((!isWhite) && isAvailable) {
				gotoAndStop(5);
			}else if ((!isWhite) && (!isAvailable)) {
				gotoAndStop(2);
			}
		}
		
		override public function toString() : String {
			return String(nameX) + " " + String(nameY);
		}
		
		public function isEmpty() : Boolean {
			return (figure == null);
		}
		
		public function getFigure() : Figure {
			return figure;
		}
	}

}